package com.example.s07_restful_swagger.controlller;

import com.example.s07_restful_swagger.pojo.Message;
import com.example.s07_restful_swagger.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/message/")
public class MessageController
{

    @Autowired
    private IMessageService messageService;

    // http://localhost:8080/message/findOne?id=1

    // http://localhost:8080/message/1
    //.http://localhost:8080/message/2

    // 接口要提供增删改查
    @GetMapping("{id}")
    public Message findOne(@PathVariable("id") Long id)
    {
        Message message = messageService.findById(id);
        return message;
    }
    @GetMapping("")
    public List<Message> findAll()
    {
        List<Message> list = messageService.findAll();
        return list;
    }

    @PostMapping("")
    public String add(Message m)
    {
        messageService.save(m);
        return "{result:\"添加成功\"}";
    }


    @DeleteMapping("{id}")
    public String del(@PathVariable("id") Long id)
    {
        messageService.removeById(id);
        return "{result:\"删除成功\"}";
    }

    @PutMapping("")
    public String update(Message m)
    {
        messageService.updateById(m);
        return "{result:\"修改成功\"}";
    }


}
